unit bairro;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, ActnList, ImgList, StdCtrls, DBCtrls, Grids,
  DBGrids, ComCtrls, ExtCtrls;

type
  TFormBairro = class(TForm)
    Bevel1: TBevel;
    Label1: TLabel;
    Label2: TLabel;
    ListViewClientes: TListView;
    DBGrid1: TDBGrid;
    QueryBairro: TQuery;
    ImageList2: TImageList;
    DataSourceBairro: TDataSource;
    ActTotal: TActionList;
    ActionIncluir: TAction;
    ActionEditar: TAction;
    ActionCancelar: TAction;
    ActionSalvar: TAction;
    ActionExcluir: TAction;
    ActionVoltar: TAction;
    QueryLista: TQuery;
    DBLookupComboBoxBairro: TDBLookupComboBox;
    DataSource1: TDataSource;
    EditValor: TEdit;
    EditBairro: TEdit;
    QueryAtualiza: TQuery;
    procedure ListViewClientesClick(Sender: TObject);
    procedure ActionVoltarExecute(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ActionIncluirExecute(Sender: TObject);
    procedure ActionSalvarExecute(Sender: TObject);
    procedure EditValorKeyPress(Sender: TObject; var Key: Char);
    procedure EditValorClick(Sender: TObject);
    procedure ActionEditarExecute(Sender: TObject);
  private
    { Private declarations }

    xid : integer;
    sql : string;
  public
    { Public declarations }
  end;

var
  FormBairro: TFormBairro;

implementation

uses Principal;

{$R *.dfm}

procedure TFormBairro.ListViewClientesClick(Sender: TObject);
var List: TListView;
begin
if TListView(Sender).Selected <> nil then
  begin
        List := TlistView(Sender);
        ActTotal.Actions[List.Selected.StateIndex].Execute;
  end;

end;

procedure TFormBairro.ActionVoltarExecute(Sender: TObject);
begin
    close;
end;

procedure TFormBairro.FormActivate(Sender: TObject);
begin
    QueryLista.Open;
    QueryBairro.Open;
end;

procedure TFormBairro.ActionIncluirExecute(Sender: TObject);
begin
    xid := 0;

    QueryLista.First;

    EditValor.text := '0';

    DBLookupComboBoxBairro.Enabled := true;
    EditValor.Enabled := true;


end;

procedure TFormBairro.ActionSalvarExecute(Sender: TObject);
begin

    if xid = 0 then
       sql := 'insert into bairro (nome,valor) values (''' + DBLookupComboBoxBairro.KeyValue + ''',' + EditValor.text + ')'
    else
       sql := 'update bairro set nome = ''' + EditBairro.text + ''', valor = ' + EditValor.text + ' where id = ' + IntToStr(xid);





    QueryAtualiza.SQL.Clear;
    QueryAtualiza.SQL.add(sql);
    QueryAtualiza.ExecSQL;

    QueryBairro.Close;
    QueryBairro.Open;

    QueryLista.Close;
    QueryLista.Open;



    DBLookupComboBoxBairro.Enabled := False;
    EditValor.Enabled := False;

    EditValor.Clear;

    DBLookupComboBoxBairro.Visible := true;
    EditBairro.Visible := false;

    xid := 0;

    self.Refresh;
end;

procedure TFormBairro.EditValorKeyPress(Sender: TObject; var Key: Char);
begin
    if not(key in ['0'..'9','.',',',#8,#13]) then
       key := #0;

    if key in [',','.'] then
       key := DecimalSeparator;

    if key = #13 then
       EditValor.Text := formatfloat('###,###,##0.00', StrToFloat(EditValor.Text) );


end;

procedure TFormBairro.EditValorClick(Sender: TObject);
begin
EditValor.SelStart := 0;
EditValor.SelLength := Length(EditValor.Text);
end;

procedure TFormBairro.ActionEditarExecute(Sender: TObject);
begin
     DBLookupComboBoxBairro.Visible := False;

     xid := QueryBairro.FieldByName('id').asinteger;

     EditBairro.Text := QueryBairro.FieldByName('nome').AsString;
     EditValor.Text := QueryBairro.FieldByName('valor').AsString;
     
     //DBLookupComboBoxBairro.Enabled := true;
     EditValor.Enabled := true;


     


end;

end.
